package face.pay.common.enterprise.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import face.pay.common.enterprise.service.IMchPayInfoService;
import face.pay.dao.MchPayInfoDao;
import face.pay.entity.EnterpriseInfo;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商家支付信息的管理
 */
@Service
public class MchPayInfoService implements IMchPayInfoService {


    @Autowired
    private MchPayInfoDao mchPayInfoDao;

    /**
     * 查询商家支付信息
     * @param loginUser
     * @param pageParam
     * @return
     */
    @Override
    public PageData queryPage(EnterpriseInfo enterpriseInfo, PageParam pageParam) {
        PageHelper.startPage(pageParam.getPageNo(),pageParam.getPageSize());


        List<EnterpriseInfo> list= this.mchPayInfoDao.queryList(enterpriseInfo);
        //截取创建时间字符
        List<EnterpriseInfo> sublist = list.stream().map(mch -> {
            String s = mch.getCreatedTime().substring(0, 19);
            mch.setCreatedTime(s);
            return mch;
        }).collect(Collectors.toList());
        PageInfo<EnterpriseInfo> pageInfo = new PageInfo <EnterpriseInfo>(list);
        PageData<EnterpriseInfo> pageData = new PageData<EnterpriseInfo>(pageInfo.getTotal(),sublist);
        return pageData;
    }

    /**
     * 编辑商家支付信息
     * @param enterpriseInfo
     * @return
     */
    @Override
    public ResultMessage editMchPayInfo(EnterpriseInfo enterpriseInfo) {
        if(enterpriseInfo.getId()==null){
            return ResultMessage.createFail("支付资料id不正确");
        }
        try {
            this.mchPayInfoDao.update(enterpriseInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMessage.createFail("编辑失败!");
        }
        return ResultMessage.createSuccess();
    }
}
